<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <el-checkbox v-model="queryData.noChapter"
                >没有章节练习
                </el-checkbox
                >
            </div>
            <div class="search-item">
                <el-checkbox v-model="queryData.noPaper">没有试卷</el-checkbox>
            </div>

            <div class="search-item">
                <el-checkbox v-model="queryData.noSpecialPractice"
                >没有随机组卷
                </el-checkbox
                >
            </div>
            <div class="search-item">
                <el-checkbox v-model="queryData.noRandomFormPaper"
                >没有专项练习
                </el-checkbox
                >
            </div>
        </div>
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>行业：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.industryId"
                        v-loading="industryFlag"
                        filterable
                        clearable
                        placeholder="请选择行业"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in industryData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>考试：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.examId"
                        v-loading="examFlag"
                        filterable
                        clearable
                        placeholder="请选择考试"
                        reserve-keyword
                    >
                        <el-option
                            v-for="item in examData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>科目名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.searchKey"
                        clearable
                        placeholder="科目名"
                    ></el-input>
                </div>
            </div>

            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="primary"
                plain
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="
                    clickAdd('添加科目', () => {
                        formExamData = formExamData || examData;
                        initAddForm.columns = defaultColumns;
                        (initAddForm.examType =
                            initAddForm.examType ||
                            queryData.examType ||
                            'zhiye'),
                            (initAddForm.examId =
                                initAddForm.examId || queryData.examId);
                        initAddForm.industryId =
                            initAddForm.industryId || queryData.industryId;
                    })
                "
            >新增数据
            </el-button>
            <el-button
                type="danger"
                plain
                style="margin-bottom: 2em"
                icon="el-icon-delete"
                :disabled="selectDisable"
                @click="
                    tableDelete(
                        '/exam/category/deletes',
                        {
                            ids: tableSelect
                        },
                        true
                    )
                "
            >批量删除
            </el-button>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                default-expand-all
                @selection-change="tableSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="科目简称(前台显示)"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="serverName"
                    label="科目全称(后台显示)"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="examName"
                    label="考试名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="industryName"
                    label="行业名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column prop="sort" label="排序" width="150">
                    <template
                        slot-scope="scope"
                        v-loading="scope.row.opLoading"
                    >
                        <div
                            v-show="!scope.row.sortEdit"
                            @dblclick="scope.row.sortEdit = true"
                        >
                            {{ scope.row.sort }}
                        </div>
                        <el-input
                            v-show="scope.row.sortEdit"
                            v-model="scope.row.sort"
                            @blur="
                                sortEditSave(
                                    '/exam/category/change-sort',
                                    scope.row
                                )
                            "
                        ></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="属性" min-width="100">
                    <template slot-scope="scope">
                        <div class="tag-table">
                            <k-tag
                                v-if="(scope.row.attribute & 2) === 2"
                                :tag="{ desc: '热门', color: 'red' }"
                            ></k-tag>
                            <k-tag
                                v-if="(scope.row.attribute & 4) === 4"
                                :tag="{ desc: '推荐', color: 'blue' }"
                            ></k-tag>
                            <k-tag
                                v-if="(scope.row.attribute & 8) === 8"
                                :tag="{ desc: '最新', color: 'green' }"
                            ></k-tag>
                            <k-tag
                                v-if="(scope.row.attribute & 16) === 16"
                                :tag="{ desc: '基础', color: 'green' }"
                            ></k-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="栏目配置" min-width="200">
                    <template slot-scope="scope">
                        <div class="column-table">
                            <k-tag
                                v-for="item in scope.row.columns"
                                :key="item.id"
                                :tag="{
                                    desc: item.name,
                                    color: columnType[item.type].color
                                }"
                            ></k-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="300"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="
                                clickSimpleEdit(
                                    scope.row,
                                    '数据编辑',
                                    undefined,
                                    editCover
                                )
                            "
                        >编辑
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="generateChapter(scope.row)"
                        >生成章节练习
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="generatePaper(scope.row)"
                        >生成试卷
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="warning"
                            type="text"
                            icon="el-icon-delete"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/exam/category/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            custom-class="two-dialog"
        >
            <el-form
                ref="dataForm"
                :key="dataFormVersion"
                :model="dataForm"
                label-width="6em"
                :rules="dataFormRule"
                class="table-edit-from"
            >
                <div class="xy-between">

                    <el-form-item
                        label="行业"
                        prop="industryId"
                    >
                        <el-select
                            v-model="dataForm.industryId"
                            placeholder="请选择行业"
                        >
                            <el-option
                                v-for="item in industryData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <div class="xy-between">
                    <el-form-item label="考试名称" prop="examId">
                        <el-select
                            v-model="dataForm.examId"
                            v-loading="examFlag"
                            filterable
                            clearable
                            placeholder="请选择考试"
                            reserve-keyword
                        >
                            <el-option
                                v-for="item in formExamData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="上级科目" prop="parentId">
                        <el-cascader
                            v-model="dataForm.parentId"
                            v-loading="categoryFlag"
                            :show-all-levels="false"
                            clearable
                            filterable
                            :props="{
                                value: 'id',
                                label: 'name',
                                emitPath: false,
                                checkStrictly: true,
                                disabled: 'haveColumn'
                            }"
                            :options="categoryData"
                            @change="
                                vl =>
                                    selectParent(
                                        vl,
                                        dataForm,
                                        dataForm.parent || tableData
                                    )
                            "
                        ></el-cascader>
                    </el-form-item>
                </div>
                <div class="row">
                    <el-form-item label="科目简称(前台显示)" prop="name">
                        <el-input v-model="dataForm.name"></el-input>
                    </el-form-item>

                    <el-form-item label="科目全称(后台显示)" prop="serverName">
                        <el-input v-model="dataForm.serverName"></el-input>
                    </el-form-item>
                </div>
                <div class="row">
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="dataForm.sort"></el-input>
                    </el-form-item>
                </div>
                <div>
                    <el-form-item :key="dataForm.attribute" label="属性">
                        <el-checkbox
                            :checked="(dataForm.attribute & 2) === 2"
                            @change="attributeChange(2)"
                        >热门
                        </el-checkbox
                        >
                        <el-checkbox
                            :checked="(dataForm.attribute & 4) === 4"
                            @change="attributeChange(4)"
                        >推荐
                        </el-checkbox
                        >
                        <el-checkbox
                            :checked="(dataForm.attribute & 8) === 8"
                            @change="attributeChange(8)"
                        >最新
                        </el-checkbox
                        >
                        <el-checkbox
                            :checked="(dataForm.attribute & 16) === 16"
                            @change="attributeChange(16)"
                        >基础
                        </el-checkbox
                        >
                    </el-form-item>
                </div>
                <div class="column-setting">
                    <el-form-item label="栏目配置">
                        <div class="xy-start title-row">
                            <p class="el-input text-center f bold">栏目名称</p>
                            <p class="sort-input text-center bold">栏目排序</p>
                            <p class="bold">栏目类型</p>
                        </div>
                        <el-checkbox
                            v-for="(item, index) in dataForm.columns"
                            :key="index"
                            v-model="item.select"
                        >
                            <el-input v-model="item.name"></el-input>
                            <el-input
                                v-model="item.sort"
                                class="sort-input"
                            ></el-input>
                            <k-tag :tag="columnType[item.type]"></k-tag>
                        </el-checkbox>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="
                        formDataSave(
                            '/exam/category/update',
                            saveDataCover,
                            () => {
                                initAddForm.examType = dataForm.examType;
                                initAddForm.industryId = dataForm.industryId;
                                initAddForm.examId = dataForm.examId;
                                initAddForm.columns = [];
                            }
                        )
                    "
                >保 存</el-button
                >
            </span>
        </el-dialog>
        <el-dialog
            :title="
                (generateChapterConditionInput || {}).categoryName + '生成章节'
            "
            :visible.sync="generateChapterVisible"
            custom-class="two-dialog"
        >
            <el-form
                v-if="generateChapterConditionInput"
                ref="dataForm"
                :model="generateChapterConditionInput"
                label-width="6em"
                :rules="generateChapterRule"
                class="table-edit-from"
            >
                <div class="xy-between">
                    <el-form-item label="来源科目" prop="questionSourceCategoryId">
                        <category-select
                            :isDisabled="true"
                            :category-id.sync="
                                generateChapterConditionInput.questionSourceCategoryId
                            "
                            :props="{}"
                        />
                    </el-form-item>
                    <el-form-item :label="viewName" prop="name">
                        <el-input
                            v-model="generateChapterConditionInput.name"
                        ></el-input>
                    </el-form-item>
                </div>
                <div>
                    <!--                    <el-form-item label="栏目" prop="columnId">-->
                    <!--                        <el-select-->
                    <!--                            v-model="generateChapterConditionInput.columnId"-->
                    <!--                            filterable-->
                    <!--                            clearable-->
                    <!--                            placeholder="请选择关联栏目"-->
                    <!--                            reserve-keyword-->
                    <!--                        >-->
                    <!--                            <el-option-->
                    <!--                                v-for="item in generateChapterColumn"-->
                    <!--                                :key="item.id"-->
                    <!--                                :label="item.name"-->
                    <!--                                :value="item.id"-->
                    <!--                            >-->
                    <!--                            </el-option>-->
                    <!--                        </el-select>-->
                    <!--                    </el-form-item>-->
                    <el-form-item label="上级章节" prop="chapterParent" v-if="type===0">
                            <el-cascader
                                v-model="paperSource.chapterId"
                                clearable
                                filterable
                                v-loading="chapterFlag"
                                :show-all-levels="false"
                                :props="{
                                    value: 'id',
                                    label: 'name',
                                   }"
                                :options="chapterData"
                            ></el-cascader>
                    </el-form-item>
                    <el-form-item label="来源试卷" prop="count" v-if="type===0">
                        <el-card class="box-card">
                            <div slot="header" class="clearfix">
                                <el-checkbox v-model="paperSource.change" >列表</el-checkbox>
                                <el-button style="float: right; padding: 3px 0" type="text">{{paperSource.checkedCities.length}}/{{paperSource.total}}</el-button>
                            </div>
                                <el-input v-model="paperSource.keyword" placeholder="请输入内容" clearable></el-input>
                                <div class="infinite-list" v-infinite-scroll="paperLoad" style="height: 200px;overflow:auto">
                                    <el-checkbox-group v-model="paperSource.checkedCities">
                                        <div v-for="data in paperSource.list" :key="data.id">
                                            <el-checkbox :label="data.id" >{{data.name}}</el-checkbox>
                                        </div>
                                    </el-checkbox-group>
                                </div>
                        </el-card>
                    </el-form-item>


                </div>
                <div class="row">
                    <el-form-item label="来源章节" v-if="type===1">
                        <el-tree
                            ref="tree"
                            :data="chapterData"
                            show-checkbox
                            @check="handleNodeClick"
                            node-key="id"
                            :props="defaultProps">
                        </el-tree>
                    </el-form-item>
                </div>
                <div class="row">
                    <el-form-item v-if="type===1">
                        <el-radio-group v-model="generateChapterConditionInput.columnName">
                            <el-radio label="模拟试卷">模拟试卷</el-radio>
                            <el-radio label="预测试卷">预测试卷</el-radio>
                            <el-radio label="冲刺试卷">冲刺试卷</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </div>
                <div class="row">
                    <el-form-item label="生成数量" prop="count">
                        <el-input
                            v-model="generateChapterConditionInput.count"
                        ></el-input>
                    </el-form-item>
                </div>
                <div v-if="generateChapterConditionInput.condition">
                    <div
                        v-for="cond in generateChapterConditionInput.condition
                            .types"
                        :key="cond.id"
                        class="row"
                    >
                        <el-form-item :label="cond.name">
                            <el-input
                                v-model="cond.count"
                                type="number"
                                min="0"
                                :max="
                                    generateChapterCondition.types.find(
                                        v => v.id === cond.id
                                    ).count
                                "
                            ></el-input>
                            <span style="margin-left: 2em;"
                            >最多{{
                                    generateChapterCondition.types.find(
                                        v => v.id === cond.id
                                    ).count
                                }}道</span
                            >
                        </el-form-item>
                    </div>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button
                    :loading="submitFlag"
                    @click="generateChapterVisible = false"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="generateChapterSubmit"
                >确定</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Fetch from "@/api/request.js";
import {TablePage, DataPage} from "@/layout/mixin/iVue.js";
import {examBase} from "@/layout/mixin/examBase";
import {mapState} from "vuex";
import {get, put, post} from "@/api/request";
import {treeDataProcess, treeList2Tree, uuid} from "@/utils";
import CategorySelect from "@/components/CategorySelect.vue";
import {fetchList} from "@/api/exam";

export default {
    name: "Category",
    components: {CategorySelect},
    mixins: [TablePage, examBase, DataPage],
    data() {
        return {
            queryData: {
                examType: null,
                searchKey: "",
                industryId: null,
                examId: null,
                tree: true,
                haveDetailName: true,
                haveColumns: true,
                noChapter: false,
                noPaper: false,
                noSpecialPractice: false,
                noRandomFormPaper: false
            },
            //0试卷 1章节
            type:0,
            viewName:"章节名称",
            changeS:false,
            formExamData: null,
            dataFormVersion: 0,
            initAddForm: {
                id: null,
                name: null,
                serverName: null,
                sort: 100,
                attribute: 0,
                industryId: null,
                examId: null,
                parentId: null,
                examType: "zhiye",
                columns: []
            },
            defaultProps: {
                children: "children",
                label: "name"
            },
            dataFormRule: {
                examId: [
                    {
                        required: true,
                        message: "请选择考试",
                        trigger: "change"
                    }
                ],
                name: [
                    {
                        required: true,
                        message: "请输入科目名称",
                        trigger: "blur"
                    }
                ]
            },
            searchPaper: {
                paperLoading: false,
                paperOptions: ""
            },
            chapterFlag:true,
            checkNode:[],
            chapterData:[],
            generateChapterCondition: {},
            generateChapterConditionInput: {},
            generateChapterColumn: [],
            generateChapterVisible: false,
            paperSource:{
                keyword:null,
                current:1,
                size:100,
                change:false,
                total:0,
                chapterId:null,
                checkedCities:[],
                list:[]
            }
        };
    },
    computed: {
        ...mapState({
            columnType: state => state.status.columnType
        }),
        generateChapterRule() {
            return {
                name: [
                    {
                        required: true,
                        message: "请输入章节名称",
                        trigger: "change"
                    }
                ]
            };
        },
        defaultColumns() {
            return Object.keys(this.columnType).map(key => {
                return {
                    name: this.columnType[key].desc,
                    type: key,
                    select: false,
                    sort: 100
                };
            });
        }
    },
    watch: {
        "queryData.industryId"(industryId) {
            this.queryData.examId = null;
            if (industryId) {
                this.loadExam(industryId);
            } else {
                this.examData = [];
            }
        },
        "dataForm.industryId"(industryId) {
            if (!this.dataForm.opLoading) {
                this.dataForm.parentId = null;
                this.dataForm.examId = null;
            }
            this.loadExam1(industryId);
        },
        "dataForm.examId"(examId) {
            if (!this.dataForm.opLoading) {
                this.dataForm.parentId = null;
            }
            if (!examId) {
                this.categoryData = [];
            } else {
                this.loadCategory1(examId);
            }
        },
        "paperSource.change"(change){
            if(change){
                this.paperSource.checkedCities=this.paperSource.list.map(m=>m.id);
            } else {
                this.paperSource.checkedCities=[];
            }
        },
        "paperSource.checkedCities"(data){
            if(this.generateChapterConditionInput.questionSourceCategoryId){
                this.changeQuestionCategory({id:this.generateChapterConditionInput.questionSourceCategoryId}, data,1);
            }
        },
        "paperSource.keyword"(keyword){
            this.paperSource.size=100
            this.changePaperList()

        },
        // // "generateChapterConditionInput.paperIds"(ids) {
        //     if (this.generateChapterConditionInput.watch) {
        //         let categoryId = this.generateChapterConditionInput.categoryId
        //         this.changeQuestionCategory({id:categoryId}, ids,1);
        //     }
        // //
        // // },
        "generateChapterConditionInput.questionSourceCategoryId"(id) {
            if (this.generateChapterConditionInput.watch) {
                this.changeQuestionCategory({id});
                this.changePaperList(id);
                this.paperSource.size=100
            }
        }
    },
    created() {
        this.loadIndustry();
    },
    mounted() {
    },
    methods: {
        remoteMethod(query) {
            if (query !== "") {
                this.searchPaper.loading = true;
                fetchList({categoryId: this.generateChapterConditionInput.categoryId, name: query}).then(res => {
                    this.searchPaper.paperOptions = res.map(item => {
                        return {label: item.name, value: item.id}
                    });
                    this.searchPaper.loading = false;
                })
            } else {
                this.paperOptions = [];
            }

        },
        paperChange(ids){
            if (this.generateChapterConditionInput.watch) {
                let categoryId = this.generateChapterConditionInput.categoryId
                //试卷类型
                this.generateChapterConditionInput.type=1
                this.generateChapterConditionInput.isScattered=false
                this.changeS=false;
                this.changeQuestionCategory({id:categoryId}, ids,1);
            }
        },
        search(queryData = {}) {
            queryData.tree =
                Boolean(!this.queryData.name) && Boolean(this.queryData.examId);
            queryData.noJgtk = this.queryData.examType === "zhiye";
            this.loadTable("/exam/category/page", queryData, true, da => {
                treeDataProcess(da, (child, parent) => {
                    child.opLoading = false;
                    child.edit = false;
                    child.sortEdit = false;
                });
                return da;
            });
        },
        loadExam1(industryId) {
            if (!industryId) {
                this.formExamData = [];
                return;
            }
            this.examFlag = true;
            get("/exam/exam/list", {
                industryId,
                size: 1000
            })
                .then(res => {
                    this.formExamData = res;
                })
                .finally(() => {
                    this.examFlag = false;
                });
        },
        loadCategory1(examId) {
            this.categoryFlag = true;
            return get("/exam/category/list", {
                examId,
                name,
                size: 1000,
                tree: false,
                columnFlag: true
            })
                .then(res => {
                    this.categoryData = treeList2Tree(res);
                    return this.categoryData;
                })
                .finally(() => {
                    this.categoryFlag = false;
                });
        },
        attributeChange(vl){
            console.log(this.dataForm.attribute+"---"+vl);
            if ((this.dataForm.attribute & vl) === vl) {
                this.dataForm.attribute =
                    this.dataForm.attribute & (0xffffffff ^ vl);
            } else {
                this.dataForm.attribute = this.dataForm.attribute | vl;
            }
        },
        editCover(row) {
            this.dataFormVersion++;
            let columns = [...(row.columns || [])];
            for (let c of this.defaultColumns) {
                if (!columns.find(v => v.type === c.type)) {
                    columns.push(c);
                }
            }
            columns.forEach(v => {
                this.$set(v, "select", v.select || Boolean(v.id));
            });
            return {
                ...row,
                columns
            };
        },
        saveDataCover(data) {
            this.dataFormVersion++;
            return {
                ...data,
                parentId: data.parentId || 0,
                columns: data.columns.filter(v => v.select)
            };
        },
        handleNodeClick(data,node){
            if (this.generateChapterConditionInput.watch) {
                let categoryId = this.generateChapterConditionInput.categoryId
                this.generateChapterConditionInput.type=2
                this.generateChapterConditionInput.ids =node.checkedKeys
                this.checkNode=node.checkedNodes;
                this.changeS=false;
                this.generateChapterConditionInput.isScattered=false
                this.changeQuestionCategory({id:categoryId},  this.generateChapterConditionInput.ids,2);
            }
            //章节类型

        },
        changeType(value){
            this.generateChapterConditionInput.isScattered=value
            if(value){
                for (let i = 0; i < this.generateChapterConditionInput.ids.length; i++) {
                    this.$refs.tree.setChecked(this.generateChapterConditionInput.ids[i],false,true)

                }
                this.generateChapterConditionInput.ids=[]
                this.generateChapterConditionInput.type=4
                this.changeQuestionCategory({id: this.generateChapterConditionInput.categoryId},  null,4);
            } else {
                this.changeQuestionCategory({id: this.generateChapterConditionInput.categoryId});
                this.generateChapterConditionInput.type=3
            }

        },
        async generateChapter(category) {
            this.paperSource.chapterId=null;
            this.paperSource.size=100;
            this.paperSource.list=[];
            this.paperSource.checkedCities=[]
            this.paperSource.change=false;
            this.paperSource.keyword=null;
            this.paperSource.current=1
            this.paperSource.total=0;
            this.type=0
            await this.generate(category)
            this.viewName="章节名称"

        },
        async generate(category){
            category.opLoading = true;
            this.generateChapterConditionInput.ids=[]
            this.generateChapterConditionInput.changeS=false;
            this.generateChapterConditionInput.watch = false;
            this.generateChapterConditionInput.categoryId = category.id;
            this.generateChapterConditionInput.categoryName = category.name;
            this.generateChapterVisible = true;
            this.changeS=false
            this.chapterFlag=true
           get(
                "/exam/chapter/list",{
                    categoryId:category.id,
                    size: 1000,
                    tree: false
                }
            ).then(res=>{
               this.chapterFlag=false
               this.chapterData = treeList2Tree(res);
               console.log(this.chapterData)
           })

            await Promise.all([
               this.changeQuestionCategory(category),
                this.changePaperList(category.id)
                // get("/exam/column/list", {
                //     categoryId: category.id,
                //     type: "chapterPractice"
                // }).then(v => (this.generateChapterColumn = v))
            ]);
            category.opLoading = false;
            this.generateChapterConditionInput.watch = true;

        },
       async changePaperList(id){
           let categoryId
            if(id){
                categoryId=id;
            }else{
                categoryId = this.generateChapterConditionInput.questionSourceCategoryId
            }
            if(!categoryId) return;
            let paperList = await get(
                "/exam/paper/paperChapterUnused",{current:this.paperSource.current,size:this.paperSource.size,keyword:this.paperSource.keyword,categoryId}
            );
           this.paperSource.list=paperList.records;
           this.paperSource.total=this.paperSource.list.length;
        },
        //generateChapter
        async generatePaper(category) {
            this.type=1
            await this.generate(category)
            this.viewName="试卷名称"
            //获取改科目下有多少章节

        },
        paperLoad(){
            this.paperSource.size= this.paperSource.size+100;
            this.changePaperList();

        },
        async changeQuestionCategory(category, ids,type) {
            let data = {
                categoryId: category.id,
                type:3
            }
            if(this.type===0){
                data.type=1
            }
            if (null !== ids && undefined !== ids) {
                data.ids = ids;
            }
            if(null!==type && undefined!==type){
                data.type=type
            }

            console.log(this.paperSource.checkedCities)
            if(!(data.type===1 && this.paperSource.checkedCities.length===0)){
                this.generateChapterCondition = await get(
                    "/exam/category/generate-condition",
                    data
                );
            }else{
                this.generateChapterCondition.types=[];
            }

            this.generateChapterCondition.types =
                this.generateChapterCondition.types || [];
            this.generateChapterCondition.types.forEach(v => {
                v.id = uuid(32);
            });
            const condition = JSON.parse(
                JSON.stringify(this.generateChapterCondition)
            );
            const input = {};
            condition.maxCount = 0;
            condition.types.forEach(v => {
                v.count = null;
                condition.maxCount += v.count;
            });
            console.log(condition)
            input.questionSourceCategoryId = category.id;
            input.condition = condition;
            input.count = null;
            if (this.generateChapterConditionInput.name === null || this.generateChapterConditionInput.name === undefined || this.generateChapterConditionInput.name === "") {
                input.name = "综合练习";
            }
            // input.columnId = "";
            this.generateChapterConditionInput = {
                ...this.generateChapterConditionInput,
                ...input,
            };
        },
        generateChapterSubmit() {
            this.submitFlag = true;
            if(this.type===0){
                this.generateChapterConditionInput.ids=this.paperSource.checkedCities;
                this.generateChapterConditionInput.chapterId = (null !== this.paperSource.chapterId && this.paperSource.chapterId.length>0) ? this.paperSource.chapterId[this.paperSource.chapterId.length-1]:null;
                post(
                    "/exam/chapter/generate-chapter",
                    this.generateChapterConditionInput
                )
                    .then(v => {
                        this.search();
                    })
                    .finally(() => {
                        this.submitFlag = false;
                        this.generateChapterVisible = false;
                    });
            }else{
                post(
                    "/exam/paper/generate-paper",
                    this.generateChapterConditionInput
                )
                    .then(v => {
                        this.search();
                    })
                    .finally(() => {
                        this.submitFlag = false;
                        this.generateChapterVisible = false;
                    });
            }

        }
    }
};
</script>

<style lang="scss" scoped>
.tag-table {
    ::v-deep .el-tag {
        margin-right: 10px;
        margin-top: 10px;
    }
}

.column-table {
    display: flex;
    flex-wrap: wrap;

    > span {
        margin-right: 10px;
        margin-bottom: 10px;
    }
}

.column-setting {
    .sort-input {
        width: 70px !important;
        margin: 0 10px;
    }

    .title-row {
        .f {
            margin-left: 24px;
        }
    }

    ::v-deep {
        .el-checkbox {
            margin-bottom: 10px;
            display: block;
        }
    }
}
</style>
